package com.bicycle.rentalsystem.service.impl.admin;

import com.bicycle.rentalsystem.dao.admin.AdminDao;
import com.bicycle.rentalsystem.dao.impl.admin.AdminDaoImpl;
import com.bicycle.rentalsystem.pojo.admin.Administrators;
import com.bicycle.rentalsystem.pojo.admin.Page;
import com.bicycle.rentalsystem.service.admin.AdminService;

import java.util.List;

/**
 * @author Third uncle
 * @date 2022/11/22 - 14:26
 */
public class AdminServiceImpl implements AdminService {
    AdminDao adminDao=new AdminDaoImpl();
    @Override
    public List<Administrators> getAdmins() {
        List<Administrators> list = adminDao.getAdministrators();
        return list;
    }

    @Override
    public void deleteAdmin(Integer id) {
        adminDao.deleteAdminById(id);
    }

    @Override
    public Administrators getAdminById(Integer aId) {
        Administrators administratorsByid = adminDao.getAdministratorsByid(aId);
        return administratorsByid;
    }

    @Override
    public void updateAdmin(Administrators admin) {
        adminDao.updateAdmin(admin);
    }

    @Override
    public Page<Administrators> page(Integer pageNo, Integer pageSize) {
        Page<Administrators> page=new Page<>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示数量
        page.setPageSize(pageSize);
        //总记录数
        Integer total = adminDao.queryForPageTotalCount();
        //设置总记录数
        page.setPageTotalCount(total);
        //总页码数
        Integer pageTotal=total/pageSize;
        if(total%pageSize>0){
            pageTotal+=1;
        }
        //设置总页码数
        page.setPageTotal(pageTotal);
        //求当前开始索引的位置
        Integer begin=(page.getPageNo()-1)*pageSize;
        //求当前页数据
        List<Administrators> list = adminDao.queryForPageItems(begin, pageSize);
        //设置当前页数据
        page.setItems(list);

        return page;
    }


}
